package crap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.RepaintManager;

import tools.Arrow;

public class Tester01 {
	public static void main(String... args) {
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final AffineTransform at = new AffineTransform();
		final Point2D center = new Point2D.Double();
		final Point2D rot = new Point2D.Double(24,24);
		
		final Arrow arrw = new Arrow();
		final AffineTransform rectTransform = AffineTransform.getRotateInstance(Math.toRadians(13));
		rectTransform.transform(rot, rot);
		rectTransform.concatenate(AffineTransform.getTranslateInstance(0.25, 0.125));
		
		final Rectangle2D rect = new Rectangle2D.Double(16,16,16,16);
		final Rectangle2D selection = new Rectangle2D.Double(16,16,16,16);
		
		final double r = Math.sqrt(rect.getWidth()*rect.getWidth()+rect.getHeight()*rect.getHeight());
		final Line2D line = new Line2D.Double();
		arrw.setArrow(new Point2D.Double(24,24), rot, 5, 10);
		final JComponent canvas = new JComponent() {
			{setPreferredSize(new Dimension(400,400));}
			@Override
			public void paint(Graphics g) {
				Graphics2D g2 = (Graphics2D)g;
				g2.transform(at);
				Shape transrect = rectTransform.createTransformedShape(rect);
				g2.setColor(Color.blue);
				g2.fill(transrect.getBounds());
				g2.setColor(Color.yellow);
				g2.fill(transrect);
//				g2.fill(rect);
				g2.setColor(Color.red);
				g2.fill(arrw);
				g2.setColor(Color.magenta);
				g2.draw(arrw);
				super.paint(g);
			}
		};
		canvas.addMouseListener(new MouseAdapter() {
			Point2D p = new Point2D.Double();
			@Override
			public void mousePressed(MouseEvent e) {
				try {
					p.setLocation(e.getX(), e.getY());
					at.inverseTransform(p, center);
				}
				catch (NoninvertibleTransformException nte) {
					nte.printStackTrace();
				}
				super.mousePressed(e);
			}
		});
		canvas.addMouseMotionListener(new MouseMotionAdapter() {
			Point2D p = new Point2D.Double();
			@Override
			public void mouseDragged(MouseEvent e) {
				try {
					p.setLocation(e.getX(), e.getY());
					p = at.inverseTransform(p, p);
					at.translate(p.getX()-center.getX(), p.getY()-center.getY());
					center.setLocation(p);
				}
				catch (NoninvertibleTransformException nte) {
					nte.printStackTrace();
				}
				canvas.repaint();
				super.mouseDragged(e);
			}
		});
		ff.add(canvas);
		
		
		ff.pack();
		ff.setVisible(true);
	}
} 
